package com.demo.netty.util;

import org.apache.log4j.Logger;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

public class Config {

    private static final Logger logger = Logger.getLogger(Config.class);

    static private Properties p;

    public static String getProperty(String pro) {
        if (p == null) {
            try {
                InputStream is = new FileInputStream("D:\\workspace\\NettyDemo\\src\\conf\\config.properties");
                InputStream httpIs = new FileInputStream("D:\\workspace\\NettyDemo\\src\\conf\\http.properties");
                InputStream tcpIs = new FileInputStream("D:\\workspace\\NettyDemo\\src\\conf\\tcp.properties");
                InputStream mqttIs = new FileInputStream("D:\\workspace\\NettyDemo\\src\\conf\\mqtt.properties");
                InputStream protobufIs = new FileInputStream("D:\\workspace\\NettyDemo\\src\\conf\\protobuf.properties");
                if (is == null) {
                    logger.error("Can not find config.properties");
                    throw new Exception("Can not find config.properties");
                }
                if (httpIs == null) {
                    logger.error("Can not find http.properties");
                    throw new Exception("Can not find http.properties");
                }
                if (tcpIs == null) {
                    logger.error("Can not find tcp.properties");
                    throw new Exception("Can not find tcp.properties");
                }
                if (mqttIs == null) {
                    logger.error("Can not find mqtt.properties");
                    throw new Exception("Can not find mqtt.properties");
                }
                if (protobufIs == null) {
                    logger.error("Can not find mqtt.properties");
                    throw new Exception("Can not find mqtt.properties");
                }
                p = new Properties();
                p.load(is);
                p.load(httpIs);
                p.load(tcpIs);
                p.load(mqttIs);
                p.load(protobufIs);
                is.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return p.getProperty(pro);
    }

    public static boolean isNull(String msg) {
        if (null == msg || "".equals(msg.trim())) {
            return true;
        }
        return false;
    }
}
